﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TP1
{
    public class Calculator
    {
        private char[] operatorChars = { '+', '-', '*', '/' };
        private List<int> numberList = new List<int>();
        private List<char> operatorList = new List<char>();

        public Calculator()
        {

        }
        public int Calculate(string expression)
        {
            anlysisString(expression);
            return getResult();
        }

        private int getResult()
        {
            int result = numberList[0];
            for (int i = 0; i < operatorList.Count; i++)
            {
                result = calculate_intern(result, numberList[i + 1], operatorList[i]);
            }
            return result;
        }

        private void anlysisString(string expression)
        {
            int numberElement = 0;
            for (int i = 0; i < expression.Length; i++)
            {
                if (Char.IsDigit(expression[i]))
                {
                    int digit = expression[i] - 48;
                    numberElement = numberElement * 10 + digit;
                }
                else
                {
                    numberList.Add(numberElement);
                    operatorList.Add(expression[i]);
                    numberElement = 0;
                }
            }
            numberList.Add(numberElement);
        }

        private int calculate_intern(int aInt, int bInt, char o)
        {
            switch (o)
            {
                case '+':
                    return aInt + bInt;
                case '-':
                    return aInt - bInt;
                case '*':
                    return aInt * bInt;
                case '/':
                    return (int)(aInt / bInt);
                default:
                    throw new Exception("Shit!");
            }
        }
    }
}

